package org.brisling.task.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.brisling.common.SysConfig;
import org.brisling.medical.domain.BaseOutpatient;
import org.brisling.medical.queryService.QueryAntidoseService;
import org.brisling.medical.queryService.QueryLargeCostService;
import org.brisling.medical.queryService.QueryLargePositiveService;
import org.brisling.medical.queryService.QueryLogicwrongService;
import org.brisling.medical.queryService.QueryMedicalPathwayPatientConclusion;
import org.brisling.medical.queryService.QueryOverlayPrescribeService;
import org.brisling.medical.service.AntiMedicalService;
import org.brisling.medical.service.BaseMedicalService;
import org.brisling.medical.service.BaseOutpatientService;
import org.brisling.medical.service.LargeCostService;
import org.brisling.medical.service.MedicalDrugCountingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MedicalScheduledService {
	private static Logger logger = LoggerFactory.getLogger(ScheduledService.class);
	// @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次  

	//antiDose比较微妙， 这里先放在query里面， 其他的一律有service与之对应
	@Autowired
	private QueryAntidoseService antiDoseSer;
	
	@Autowired
	private AntiMedicalService antiMedicalSer;
	
	@Autowired
	private BaseMedicalService baseMedicalSer;

	@Autowired
	private QueryOverlayPrescribeService qryOverlayPrescribeService;
	
	@Autowired
	private QueryLargeCostService qryLargeCostService;
	
	@Autowired
	private LargeCostService LargeCostService;

	@Autowired
	private BaseOutpatientService baseOutpatientService;
	
	@Autowired
	private QueryLogicwrongService qryLogicwrongService;
	@Autowired
	private MedicalDrugCountingService medicalDrugCountingService;
	@Autowired
	private QueryMedicalPathwayPatientConclusion qryMedicalPathwayPatientColService;
	@Autowired
	private QueryLargePositiveService qrylargePositiveService;
	
	/*
	 * 月度执行的部分
	 */
	//基本信息首先更新
	@Scheduled(cron="0 30 1 * * ?")
	public void baseOutpatient() {
		//禁用日志
		SysConfig.setEnableLog(false);
		
		List<BaseOutpatient> lists = baseOutpatientService.autoUpdata(null, null, null, null, null, null);
	}
	
	//每月一号查询上个月信息， 用药强度异常质询
	@Scheduled(cron="0 30 2 1 * ?")
	public void antiDoseCount(){
		//System.out.println("\n\n\nhaha, got one AntiDose\n\n\n");
		antiDoseSer.autoInsertData();
		//System.out.println("\n\n\nhaha, got one AntiDose\n\n\n");
	}

	//每月一号查询上个月信息， 用药率异常质询
	@Scheduled(cron="0 35 2 1 * ?")
	public void AntiMedicalCount(){
		//System.out.println("\n\n\nhaha, got one AntiMedical\n\n\n");
		antiMedicalSer.autoInsertData();
		//System.out.println("\n\n\nhaha, got one AntiMedical\n\n\n");
	}

	
	
	/*
	 * 每日执行的部分(临床路径)
	 */

	@Scheduled(cron="0 0 2 ? * *")
    public void pathwayPatientColCount(){
		//禁用日志
		SysConfig.setEnableLog(false);
		
		String threadName =Thread.currentThread().getName();
		System.out.println("进入测试: 线程 - " +threadName); 
		Date time_start = Calendar.getInstance().getTime();
		logger.debug("PATH WAY start at:" +Calendar.getInstance().getTime());
		qryMedicalPathwayPatientColService.autoMonitor(null, null);
		logger.debug("PATH WAY END at :" + Calendar.getInstance().getTime());
		Date time_stop = Calendar.getInstance().getTime();
		Long seconds = time_stop.getTime() - time_start.getTime();
		logger.debug("PATH WAY time consume:" + seconds);
	}
	
	/**
	 * 没日执行(超限处方)
	 */
	@Scheduled(cron="0 0 2 ? * *")
	public void OverlayPrescribeCountFig(){
		String threadName =Thread.currentThread().getName();
		System.out.println("进入测试: 线程 - " +threadName); 
		Date time_start = Calendar.getInstance().getTime();
		logger.debug("ANTI MEDICAL start at:" +Calendar.getInstance().getTime());
		qryOverlayPrescribeService.autoMonitor(null, null);
		logger.debug("ANTI MEDICAL END at :" + Calendar.getInstance().getTime());
		Date time_stop = Calendar.getInstance().getTime();
		Long seconds = time_stop.getTime() - time_start.getTime();
		logger.debug("ANTI MEDICAL time consume:" + seconds);
	 }
	
	//每天查询上一天的记录， 超限处方
	@Scheduled(cron="0 10 2 * * ?")
	public void OverlayPrescribeCount() {
		qryOverlayPrescribeService.autoInsertData(null);
	}
	//大额费用
	@Scheduled(cron="0 15 2 * * ?")
	public void QueryLargecostService() {
		String id =SysConfig.getConfig("inquiryUserId");
		qryLargeCostService.autosave(id);
	}
	
	//大额费用计算
	@Scheduled(cron="0 20 2 * * ?")
	public void LargeCostService() {
		String id =SysConfig.getConfig("inquiryUserId");
		LargeCostService.autoRecord(id);
	}
	
	//逻辑错误
	@Scheduled(cron="0 25 2 * * ?")
	public void QueryLogicwrongService() {
		String id =SysConfig.getConfig("inquiryUserId");
		qryLogicwrongService.autosave(id, null);
	}	 
	
	//每月一号查询上个月信息， 基本药物使用金额占比异常质询
	@Scheduled(cron="0 30 2 1 * ?")
	public void BaseMedicalCount(){
		//System.out.println("\n\n\nhaha, got one AntiMedical\n\n\n");
		baseMedicalSer.autoInsertData();
		//System.out.println("\n\n\nhaha, got one AntiMedical\n\n\n");
	}
	
	@Scheduled(cron="0 35 2 * * ?")
	public void QueryMedicalDrugCountingService() {
		medicalDrugCountingService.autoMonitor(null, null);
	}
	
	@Scheduled(cron="0 40 2 ? * * ")
	public void antiMedicalLargePositiveCount(){
		System.out.println("\n\n\nhaha, antiMedicalLargePositive      got one autocarry\n\n\n");
		qrylargePositiveService.autoTask();
		
		//启用日志
		SysConfig.setEnableLog(true);
	}
	
}
